package cn.py.boot_lua.controller.handler;

import cn.py.boot_lua.handler.admin.RequestTask;
import cn.py.boot_lua.handler.generic.GenericQueueService;
import cn.py.boot_lua.handler.generic.QueueType;
import cn.py.boot_lua.service.asynser.AsynserviceImpl;
import cn.py.boot_lua.util.HttpUtils;
import cn.py.boot_lua.util.exception.BusinessException;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/handler")
public class HandlerController {
    @Autowired
    private GenericQueueService genericQueueService;
    @Autowired
    private AsynserviceImpl asynservice;

    @PostMapping("/submit")
    public String submitParameter(@RequestBody String json) {
        if (genericQueueService.getQueueSize(QueueType.IMG_CAP.getKey()) >= 5) {
            return "访问人数过多,请稍后再试";
        }
        JSONObject jsonObject = JSONObject.parseObject(json);
        String requestId = UUID.randomUUID().toString();
        RequestTask requestTask = new RequestTask();
        requestTask.setRequestId(requestId);
        requestTask.setRequestData(String.valueOf(jsonObject));
        log.info("RequestTask 图配文数据为: " + requestTask);
        genericQueueService.addToQueue(QueueType.IMG_CAP.getKey(), requestTask);
        return requestId;
    }

    @PostMapping("/queue")
    public String queue(@RequestBody String json) {
        if (genericQueueService.getQueueSize(QueueType.STORY.getKey()) >= 5) {
            return "访问人数过多,请稍后再试";
        }
        JSONObject jsonObject = JSONObject.parseObject(json);
        String requestId = UUID.randomUUID().toString();
        RequestTask requestTask = new RequestTask();
        requestTask.setRequestId(requestId);
        requestTask.setRequestData(String.valueOf(jsonObject));
        log.info("RequestTask 生成故事数据为: " + requestTask);
        genericQueueService.addToQueue(QueueType.STORY.getKey(), requestTask);
        return requestId;
    }

    @PostMapping("/back")
    public String back(@RequestBody String json) {
        if (genericQueueService.getQueueSize(QueueType.BACK_IMG.getKey()) >= 5) {
            return "访问人数过多,请稍后再试";
        }
        JSONObject jsonObject = JSONObject.parseObject(json);
        String requestId = UUID.randomUUID().toString();
        RequestTask requestTask = new RequestTask();
        requestTask.setRequestId(requestId);
        requestTask.setRequestData(String.valueOf(jsonObject));
        log.info("RequestTask 图像背景数据为: " + requestTask);
        genericQueueService.addToQueue(QueueType.BACK_IMG.getKey(), requestTask);
        return requestId;
    }

    @PostMapping("/sun")
    public String sun(@RequestBody JSONObject json) {
        if (json == null){
            throw new BusinessException("请求参数不能为空！");
        }
        asynservice.submitVideoDubbingTask101(json);
        return "加入成功";
    }


}
